package cc.rengu.igas.smps.biz.service.impl;

import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.MsgPushService;
import cc.rengu.igas.smps.facade.request.AliyunIotMsgPushRequest;
import cc.rengu.igas.smps.facade.request.InspiryVoicePushRequest;
import cc.rengu.igas.smps.facade.request.SunyardVoicePushRequest;
import cc.rengu.igas.smps.facade.request.TransResultNoticeRequest;
import cc.rengu.igas.smps.facade.response.AliyunIotMsgPushResponse;
import cc.rengu.igas.smps.facade.response.InspiryVoicePushResponse;
import cc.rengu.igas.smps.facade.response.SunyardVoicePushResponse;
import cc.rengu.igas.smps.facade.response.TransResultNoticeResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by 王成 on 2020/2/27
 */
public class MsgPushServiceImpl implements MsgPushService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<TransResultNoticeResponse> mchntTransResultNotice(TransResultNoticeRequest transResultNoticeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TransResultNoticeResponse> result = new Result<>();
        TransResultNoticeResponse transResultNoticeResponse = new TransResultNoticeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(transResultNoticeRequest, SmpsServiceConstant.TRANS_RESULT_NOTICE_SERVICE, TransResultNoticeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TransResultNoticeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易结果通知失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transResultNoticeResponse.setRespCode(((BizException) e).getCode());
                transResultNoticeResponse.setRespDesc(e.getMessage());
            } else {
                transResultNoticeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transResultNoticeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliyunIotMsgPushResponse> aliyunIotMsgPush(AliyunIotMsgPushRequest aliyunIotMsgPushRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliyunIotMsgPushResponse> result = new Result<>();
        AliyunIotMsgPushResponse aliyunIotMsgPushResponse = new AliyunIotMsgPushResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliyunIotMsgPushRequest, SmpsServiceConstant.ALIYUN_IOT_MSG_PUSH_SERVICE, AliyunIotMsgPushResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliyunIotMsgPushResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("阿里云物联网消息推送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                aliyunIotMsgPushResponse.setRespCode(((BizException) e).getCode());
                aliyunIotMsgPushResponse.setRespDesc(e.getMessage());
            } else {
                aliyunIotMsgPushResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliyunIotMsgPushResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<SunyardVoicePushResponse> sunyardVoicePush(SunyardVoicePushRequest sunyardVoicePushRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<SunyardVoicePushResponse> result = new Result<>();
        SunyardVoicePushResponse sunyardVoicePushResponse = new SunyardVoicePushResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(sunyardVoicePushRequest, SmpsServiceConstant.SUNYARD_VOICE_PUSH_SERVICE, SunyardVoicePushResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((SunyardVoicePushResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("信雅达语音消息推送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                sunyardVoicePushResponse.setRespCode(((BizException) e).getCode());
                sunyardVoicePushResponse.setRespDesc(e.getMessage());
            } else {
                sunyardVoicePushResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                sunyardVoicePushResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<InspiryVoicePushResponse> inspiryVoicePush(InspiryVoicePushRequest inspiryVoicePushRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<InspiryVoicePushResponse> result = new Result<>();
        InspiryVoicePushResponse inspiryVoicePushResponse = new InspiryVoicePushResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(inspiryVoicePushRequest, SmpsServiceConstant.INSPIRY_VOICE_PUSH_SERVICE, InspiryVoicePushResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((InspiryVoicePushResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("意锐语音消息推送失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                inspiryVoicePushResponse.setRespCode(((BizException) e).getCode());
                inspiryVoicePushResponse.setRespDesc(e.getMessage());
            } else {
                inspiryVoicePushResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                inspiryVoicePushResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
